package com.df.ez_game.server.logic;

import com.alibaba.fastjson2.JSON;
import com.df.ez_game.EZGameCtx;
import com.df.ez_game.server.IMsgFilter;
import com.df.ez_game.server.NodeType;
import com.df.ez_game.server.ServerNode;
import com.df.ez_game.server.req_metadata.EzControllerData;
import com.df.ez_game.server.req_metadata.EzMethodData;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DebugMsgFilter implements IMsgFilter {
    @Override
    public void oneBefore(EZGameCtx ctx) {

    }

    @Override
    public void onAfter(EZGameCtx ctx) {

    }

    @Override
    public void onError(EZGameCtx ctx) {

    }

    @Override
    public void onComplete(EZGameCtx ctx) {
//            ┏━━━━━ REQ ━━━━━ [ver:0]━━━━━ [cmd:1-0 0x1001] ━━━━━ [size:128B] ━━━━━ [cost:12.3ms] ━━━━━━
//            ┣ 位置: DemoAction.java:5 (here)
//            ┣ 用户: userId=1 | traceId=956230991452569600 | logicSvr=xxx逻辑服 | connId=76526c134cc
//            ┣ 参数: LoginVerifyMessage(jwt=hello, device="iOS", version=3.2.1)
//            ┣ 响应: UserMessage(name="Michael Jackson", gold=1500, items=[301,205])
//            ┣ 耗时: 业务=1ms | 网络=11.3ms | 总=12.3ms
//            ┣ 结果: fail | reason=参数解析错误
//            ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
        Integer cmd = ctx.getReqMsg().getCmd();
        Integer reqId = cmd&0xffff;
        Integer module = (cmd>>16)&0xffff;

        EzMethodData methodData = ctx.getMethodData();
        ServerNode serverNode = ctx.getServerNode();

        StringBuilder locate = new StringBuilder("");
        LogicServerNode lsn = (LogicServerNode) serverNode;
        EzControllerData ezControllerData = lsn.getMetadatas().getDatas().get(module);
        String controllerClassName = ezControllerData.getControllerName();
        locate.append(controllerClassName).append(".java");
        locate.append("[ ").append(methodData.getMethodName()).append(" ]");

        String tranceId = ctx.getReqMsg().getTranceId();
        tranceId = tranceId==null?"":tranceId;

        StringBuilder sb = new StringBuilder();
        sb.append("\n");
        sb.append("┏━━━━━ REQ ━━━━━ [ver:0]━━━━━ [cmd:").append(module).append("-").append(reqId).append(" ").append(cmd).append("] ━━━━━").append("\n");
        sb.append("┣ 位置: ").append(locate.toString()).append("\n");
        sb.append("┣ 用户: ").append("userId=")
                .append(ctx.getSession().getUserId()).append(" | ")
                .append("traceId=").append(tranceId).append(" | ")
                .append("serverName=").append(serverNode.getName()).append("(id=)").append(" | ").append("channelId:").append(ctx.getChannel().id().asLongText())
                        .append("\n");
        String paramStr = "";
        if(ctx.getReqParam()!=null){
            paramStr = JSON.toJSONString(ctx.getReqParam());
        }
        sb.append("┣ 参数: ").append(methodData.getParamType().getSimpleName()).append("(").append(paramStr).append(")").append("\n");
        String returnStr = "";
        if(ctx.getRespObj()!=null){
            returnStr = JSON.toJSONString(ctx.getRespObj());
        }
        String returnClassName = "";
        if(methodData.getReturnType()!=null){
            returnClassName = methodData.getReturnType().getSimpleName();
        }
        sb.append("┣ 响应: ").append(returnClassName).append("(").append(returnStr).append(")").append("\n");
        sb.append("┣ 耗时: ").append(ctx.getReqEndTime()-ctx.getReqStartTime()).append("ms").append("\n");
        sb.append("┣ 结果: ").append(ctx.getStatus());
        if(ctx.getStatus().equals("fail")){
            sb.append(" | ").append("reason:").append(ctx.getReason());
        }
        sb.append("\n");
        sb.append("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
        log.info(sb.toString());
    }
}
